package com.zeratul.common.pool.config.thread;

import com.zeratul.common.core.constant.PoolConstants;
import com.zeratul.common.pool.utils.ThreadUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;

import java.util.concurrent.Executor;

/**
 * 线程池配置
 */
@Slf4j
@Configuration
public class CoreThreadPoolConfig {

    /**
     * 核心线程数 = cpu 核心数
     */
    private final int core = Runtime.getRuntime().availableProcessors();

    /**
     * 线程池大小
     */
    private final int poolSize = 1000;

    /**
     * 核心线程池配置
     */
    @Lazy
    @Bean(name = PoolConstants.CORE_POOL)
    protected Executor coreExecutorService() {
        //创建线程池
        return ThreadUtils.newThreadPool(core * 2,
                core * 4,
                60, poolSize,
                "core-pool",
                true);
    }
}
